﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace AppDesigner.XamlDom
{
    public class XamlDocument
    {
        #region Fields

        private XmlDocument _xmlDoc; //XmlDocument
        private XamlParserSettings _xamlParserSettings;//settings
        private IServiceProvider _serviceProvider;
        private XamlObject _rootXamlObject;

        #endregion

        #region Cosnsturctor


        public XamlDocument(XmlDocument xmlDoc, XamlParserSettings xamlParserSettings)
        {
            this._xmlDoc = xmlDoc;
            this._xamlParserSettings = xamlParserSettings;
        }

        #endregion

        #region Properties

        public XmlDocument XmlDocument
        {
            get { return _xmlDoc; }
        }

        public IServiceProvider ServiceProvider
        {
            get { return _serviceProvider; }
        }

        public XamlObject RootXamlObject
        {
            get
            {
                return _rootXamlObject;
            }
        }

        public object RootInstance
        {
            get
            {
                return (_rootXamlObject != null) ? _rootXamlObject.Instance : null;
            }
        }

        #endregion

        #region Method

        /// <summary>
        /// 获取类型解释其的上下文
        /// </summary>
        /// <param name="containingObject"></param>
        /// <returns></returns>
        public ITypeDescriptorContext GetTypeDescriptorContext(XamlObject containingObject)
        {
            IServiceProvider serviceProvider;
            if (containingObject != null)
            {
                if (containingObject.OwnerDocument != this)
                {
                    throw new ArgumentException("Containing object must belong to the document!");
                }
                serviceProvider = containingObject.ServiceProvider;
            }
            else
            {
                serviceProvider = this._serviceProvider;
            }
            return new CommonTypeDescriptorContext(serviceProvider);
        }

        /// <summary>
        /// 解析完成后的操作
        /// </summary>
        /// <param name="root"></param>
        public void ParseComplete(XamlObject root)
        {
            _rootXamlObject = root;
        }

        public string GetNamespaceFor(Type type)
        {
            return XamlTypeFinder.GetXmlNamespaceFor(type.Assembly, type.Namespace);
        }

        #endregion
        
    }

    #region CommonTypeDescriptorContext 一般的TypeDescriptorContext

    public class CommonTypeDescriptorContext : ITypeDescriptorContext
    {
        private readonly IServiceProvider baseServiceProvider;

        public CommonTypeDescriptorContext(IServiceProvider serviceProvider)
        {
            baseServiceProvider = serviceProvider;
        }

        public IContainer Container
        {
            get { return null; }
        }

        public object Instance { get; set; }

        public void OnComponentChanged()
        {

        }

        public bool OnComponentChanging()
        {
            return false;
        }

        public PropertyDescriptor PropertyDescriptor
        {
            get { return null; }
        }

        public object GetService(Type serviceType)
        {
            return baseServiceProvider.GetService(serviceType);
        }
    }

    #endregion
    
}
